import { defineAsyncComponent } from 'vue'
const regCom = (res) => {
  return defineAsyncComponent(() => {
    let a = new Promise((resolve, reject) => {
      let reg = /<template>([\s|\S]*)<\/template>[\s|\S]*export default([\s|\S]*)<\/script>/g
      let b = reg.exec(res)
      if (!b) {
        reject({
          template: '<div>无效的组件</div>',
        })
      }
      b[2] = b[2].replace(/[\s|\r|\n|]/g, '')
      b[2] = b[2].replace(/Number/g, '"Number"')
      b[2] = b[2].replace(/Array/g, '"Array"')
      b[2] = b[2].replace(/Boolean/g, '"Boolean"')
      b[2] = b[2].replace(/String/g, '"String"')
      b[2] = b[2].replace(/Object/g, '"Object"')
      b[2] = JSON.parse(b[2])
      let ty = {
        Number: Number,
        Boolean: Boolean,
        String: String,
        Array: Array,
        Object: Object,
      }
      for (let item in b[2].props) {
        b[2].props[item].type = ty[b[2].props[item].type]
      }
      resolve({
        template: b[1],
        props: b[2].props,
      })
    })
    return a
  })
}
export default regCom
